package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.mapper.SysLogininforMapper;
import com.ruoyi.system.service.ISysLogininforService;

/**
 * 系统访问日志情况信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysLogininforServiceImpl implements ISysLogininforService
{

    @Autowired
    private SysLogininforMapper logininforMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 新增系统登录日志
     * 
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor)
    {

        logininfor.setLoginTime(new Date());

        SysUser user = userService.selectUserByUserName(logininfor.getUserName());

        if(StringUtils.isNotNull(user)&&StringUtils.isNotEmpty(user.getCompanyCod())){
            logininfor.setCompanyCode(user.getCompanyCod());
        }

        logininforMapper.insert(logininfor);
    }

    /**
     * 查询系统登录日志集合
     * 
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor)
    {

        QueryWrapper<SysLogininfor> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .like(StringUtils.isNotEmpty(logininfor.getIpaddr()),SysLogininfor::getIpaddr,logininfor.getIpaddr())
                .eq(StringUtils.isNotEmpty(logininfor.getStatus()),SysLogininfor::getStatus,logininfor.getStatus())
                .like(StringUtils.isNotEmpty(logininfor.getUserName()),SysLogininfor::getUserName,logininfor.getUserName())
                .apply(StringUtils.isNotNull(logininfor.getParams().get("beginTime")),"login_time >= {0}", DateUtils.parseDate(logininfor.getParams().get("beginTime")))
                .apply(StringUtils.isNotNull(logininfor.getParams().get("endTime")),"login_time <= {0} ", DateUtils.parseDate(logininfor.getParams().get("endTime")))
                .orderByDesc(SysLogininfor::getInfoId);

        List<SysLogininfor> list = logininforMapper.selectList(queryWrapper);

        return list;
    }

    /**
     * 批量删除系统登录日志
     * 
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds)
    {
        return logininforMapper.deleteLogininforByIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        logininforMapper.cleanLogininfor();
    }
}
